import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:oramemo/common/styles/colors.dart';
import 'package:oramemo/entity/device_mine_entity.dart';
import 'package:oramemo/generated/assets.dart';
import 'package:oramemo/pages/facility/blue/blue_logic.dart';
import 'package:oramemo/pages/facility/facility_router.dart';
import 'package:oramemo/pages/home/home_logic.dart';
import 'package:oramemo/widget/my_button.dart';

import 'blue_setting_logic.dart';

class BlueSettingPage extends StatelessWidget {
  const BlueSettingPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.put(BlueSettingLogic());
    final blueLogic = Get.put(BlueLogic());

    return Scaffold(
      appBar: AppBar(
        title: Text(logic.device?.device?.deviceName ?? '--'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios),
          onPressed: () => Get.back(),
        ),
        actions: [
          IconButton(
            icon: Icon(Icons.more_horiz),
            onPressed: () {
              Get.toNamed(FacilityRouter.deviceDetailPage,
                  arguments: logic.device);
            },
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: GetBuilder<BlueSettingLogic>(builder: (logic) {
          return Column(
            children: [
              // 蓝牙连接状态
              Align(
                alignment: Alignment.centerRight,
                child: InkWell(
                  onTap: logic.connectBluetooth,
                  child: Padding(
                    padding: EdgeInsets.only(
                        left: 16.w, right: 16.w, top: 30.h, bottom: 16.h),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Icon(
                          Icons.bluetooth,
                          color: logic.isConnected.value
                              ? Colors.blue
                              : Colours.text_gray,
                          size: 20.sp,
                        ),
                        SizedBox(width: 4.w),
                        Obx(() {
                          return Text(
                            logic.isConnected.value ? '蓝牙已连接' : '蓝牙未连接',
                            style: TextStyle(
                              color: Colours.text_gray,
                              fontSize: 14.sp,
                            ),
                          );
                        }),
                      ],
                    ),
                  ),
                ),
              ),

              // 左右耳电量显示
              Container(
                margin: EdgeInsets.fromLTRB(16.w, 0, 16.w, 16.h),
                padding: EdgeInsets.all(16.w),
                decoration: BoxDecoration(
                  color: Color(0xFFF3F5F9),
                  borderRadius: BorderRadius.circular(36.r),
                ),
                child: Row(
                  children: [
                    // 左耳电量
                    Expanded(
                      child: _buildEarPanel(
                        title: '左耳电量',
                        label: 'L',
                        batteryValue: '- -',
                        onSubTap: () {
                          // 减小音量
                        },
                        onAddTap: () {
                          // 增加音量
                        },
                      ),
                    ),

                    // 分隔线
                    SizedBox(
                      height: 120.h,
                      width: 12,
                    ),

                    // 右耳电量
                    Expanded(
                      child: _buildEarPanel(
                        title: '右耳电量',
                        label: 'R',
                        batteryValue: '- -',
                        onSubTap: () {
                          // 减小音量
                        },
                        onAddTap: () {
                          // 增加音量
                        },
                      ),
                    ),
                  ],
                ),
              ),

              SizedBox(height: 18.h),
              // 模式选择标题
              Center(
                child: Text(
                  '模式选择',
                  style: TextStyle(
                    color: Colors.blue,
                    fontSize: 18.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),

              SizedBox(height: 12.h),

              // 自定义模式
              _buildModeCard(
                title: '自定义模式',
                subtitle: '自由调控 + 个性化适配',
                isSelected: logic.selectedMode.value == 0,
                hasIcon: true,
                onTap: () async {
                  logic.setMode(0);
                  final String res = await Get.toNamed(
                      FacilityRouter.customAdjustmentPage,
                      arguments: logic.device);
                  if (res?.isNotEmpty == true) {
                    logic.device?.device?.deviceConfig = res;
                  }
                },
              ),

              SizedBox(height: 16.h),

              // 强降噪模式
              _buildModeCard(
                title: '强降噪模式',
                subtitle: '适用场景：地铁站、工地、演唱会等\n超嘈杂环境。',
                isSelected: logic.selectedMode.value == 1,
                onTap: () {
                  // 选择强降噪模式
                  logic.setMode(1);
                },
              ),

              SizedBox(height: 16.h),

              // 室内模式
              _buildModeCard(
                title: '室内模式',
                subtitle: '家庭客厅、办公室、会议室等\n中小型室内空间。',
                isSelected: logic.selectedMode.value == 2,
                onTap: () {
                  // 选择室内模式
                  logic.setMode(2);
                },
              ),

              SizedBox(height: 16.h),

              // 室外模式
              _buildModeCard(
                title: '室外模式',
                subtitle: '适用场景：街道步行、公园、骑行等\n开放户外环境。',
                isSelected: logic.selectedMode.value == 3,
                onTap: () {
                  // 选择室外模式
                  logic.setMode(3);
                },
              ),

              SizedBox(height: 32.h),
            ],
          );
        }),
      ),
    );
  }

  Widget _buildEarPanel({
    required String title,
    required String label,
    required String batteryValue,
    required VoidCallback onSubTap,
    required VoidCallback onAddTap,
  }) {
    return Container(
      padding: EdgeInsets.fromLTRB(16.w, 20.h, 16.w, 14.h),
      decoration: BoxDecoration(
        color: Color(0xFFFFFFFF),
        borderRadius: BorderRadius.circular(24.r),
      ),
      child: Column(
        children: [
          // 标题和标签
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                title,
                style: TextStyle(
                  color: Color(0xFF06164B),
                  fontSize: 20.sp,
                  fontWeight: FontWeight.w500,
                ),
              ),
              SizedBox(width: 20.w),
              Container(
                width: 20.w,
                height: 20.w,
                decoration: BoxDecoration(
                  color: Color(0xFFAAAAAA),
                  shape: BoxShape.circle,
                ),
                child: Center(
                  child: Text(
                    label,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
            ],
          ),

          SizedBox(height: 5.h),

          // 电量显示
          IconX.image(
            Assets.facilityEar,
            width: 35.w,
            height: 61.h,
          ),

          SizedBox(height: 8.h),

          // 音量控制
          Container(
            width: 120.w,
            height: 32.h,
            decoration: BoxDecoration(
              color: Color(0xFFEEEEEF),
              borderRadius: BorderRadius.circular(12.r),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                // 减小音量
                InkWell(
                  onTap: onSubTap,
                  child: Container(
                    width: 40.w,
                    height: 40.h,
                    child: Icon(
                      Icons.volume_down,
                      color: Color(0xFF888992),
                    ),
                  ),
                ),

                // 音量显示
                Text(
                  '- -',
                  style: TextStyle(
                      color: Colors.black,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.bold),
                ),

                // 增加音量
                InkWell(
                  onTap: onAddTap,
                  child: Container(
                    width: 40.w,
                    height: 40.h,
                    child: Icon(
                      Icons.volume_up,
                      color: Color(0xFF888992),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildModeCard({
    required String title,
    required String subtitle,
    required bool isSelected,
    bool hasIcon = false,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 16.w),
        padding: hasIcon
            ? EdgeInsets.fromLTRB(9.w, 29.h, 9.w, 17.h)
            : EdgeInsets.fromLTRB(9.w, 12.h, 9.w, 9.h),
        decoration: BoxDecoration(
          color: Color(0xFFF3F5F9),
          borderRadius: BorderRadius.circular(36.r),
          border: isSelected ? Border.all(color: Colors.blue, width: 2) : null,
          boxShadow: isSelected
              ? [
                  BoxShadow(
                    color: Colors.blue.withOpacity(0.1),
                    blurRadius: 8,
                    offset: Offset(0, 2),
                  )
                ]
              : null,
        ),
        child: Row(
          children: [
            if (hasIcon)
              IconX.image(
                Assets.facilityModeleft,
                width: 142.w,
                height: 74.h,
              ),
            SizedBox(width: hasIcon ? 16.w : 0),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        title,
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: hasIcon ? 24.sp : 20.sp,
                          fontWeight: FontWeight.normal,
                        ),
                      ),
                      SizedBox(width: 11.w),
                      if (hasIcon)
                        IconX.image(
                          Assets.facilityModeright,
                          width: 25.w,
                          height: 25.h,
                        ),
                    ],
                  ),
                  SizedBox(height: hasIcon ? 9.h : 3.h),
                  Text(
                    subtitle,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Color(0xFF888992),
                      fontSize: hasIcon ? 14.sp : 10.sp,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
